package com.springboot.base.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.springboot.base.common.BusinessException;
import com.springboot.base.common.EmBusinessError;
import com.springboot.base.common.ServerResponse;
import com.springboot.base.model.Category;
import com.springboot.base.model.Shop;
import com.springboot.base.service.ICategoryService;
import com.springboot.base.service.IShopService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author meiguangya
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private IShopService shopService;

    @Autowired
    private ICategoryService categoryService;

    /**
     * 推荐服务V1.0
     */
    @RequestMapping("/recommend")
    @ResponseBody
    public ServerResponse recommend(@RequestParam(name="longitude")BigDecimal longitude,
                                    @RequestParam(name="latitude")BigDecimal latitude) throws BusinessException {
        if(longitude == null || latitude == null){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }

        List<Shop> shopList = shopService.recommend(longitude,latitude);
        return ServerResponse.createBySuccessData(shopList);
    }

    /**
     * 搜索服务V1.0
     */
    @RequestMapping("/search")
    @ResponseBody
    public ServerResponse search(@RequestParam(name="longitude")BigDecimal longitude,
                            @RequestParam(name="latitude")BigDecimal latitude,
                            @RequestParam(name="keyword")String keyword,
                            @RequestParam(name="orderby",required = false)Integer orderby,
                            @RequestParam(name="categoryId",required = false)Integer categoryId,
                            @RequestParam(name="tags",required = false)String tags) throws BusinessException, IOException {
        if(StringUtils.isEmpty(keyword) || longitude == null || latitude == null){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }

        Map<String,Object> resultMap = shopService.searchByEsLowerLeverl(longitude,latitude,keyword,orderby,categoryId,tags);
        List<Shop> shopModelList = (List<Shop>)resultMap.get("shop");
        List<Category> categoryModelList = categoryService.selectAll();
        //List<Map<String,Object>> tagsAggregation = shopService.searchGroupByTags(keyword,categoryId,tags);
        List<Map<String,Object>> tagsAggregation = (List<Map<String,Object>>)resultMap.get("tags");
        Map<String,Object> resMap = new HashMap<>();

        resMap.put("shop",shopModelList);
        resMap.put("category",categoryModelList);
        resMap.put("tags",tagsAggregation);
        return ServerResponse.createBySuccessData(resMap);

    }

}
